#include <iostream>  
  
using namespace std;  
  
struct ListNode {  
    int val;  
    ListNode *next;  
    ListNode(int x) : val(x), next(NULL) {}  
};  
  
ListNode* reverseList(ListNode* head) {  
    ListNode* prev = NULL;  
    ListNode* current = head;  
    while (current != NULL) {  
        ListNode* next = current->next;  
        current->next = prev;  
        prev = current;  
        current = next;  
    }  
    return prev;  
}  
  
void printList(ListNode* head) {  
    ListNode* current = head;  
    while (current != NULL) {  
        cout << current->val << " ";  
        current = current->next;  
    }  
    cout << endl;  
}  
  
int main() {  
    ListNode* head = new ListNode(1);  
    head->next = new ListNode(2);  
    head->next->next = new ListNode(3);  
    head->next->next->next = new ListNode(4);  
    head->next->next->next->next = new ListNode(5);  
    head->next->next->next->next->next = new ListNode(6);  
      
    cout << "Original list: ";  
    printList(head);  
      
    head = reverseList(head);  
      
    cout << "List after reversing: ";  
    printList(head);  
      
    return 0;  
}